package com.qingzhuge.member.service.impl;

import com.qingzhuge.common.utils.IpUtils;
import com.qingzhuge.common.utils.WebUtil;
import com.qingzhuge.member.dao.SysMemberThirdOauthDao;
import com.qingzhuge.member.dto.UserLoginResponse;
import com.qingzhuge.member.entity.SysMember;
import com.qingzhuge.member.entity.SysMemberThirdPartyAuth;
import com.qingzhuge.member.enums.EnumThirdOauthType;
import com.qingzhuge.member.service.ISysMemberService;
import com.qingzhuge.member.service.ISysMemberThirdOauthService;
import com.qingzhuge.service.BasePkLongServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2018/8/8 0008 下午 16:57
 * @modified :
 * @since :
 */
@Service
public class SysMemberThirdOauthServiceImpl extends BasePkLongServiceImpl<SysMemberThirdOauthDao, SysMemberThirdPartyAuth> implements ISysMemberThirdOauthService {
    @Resource
    private ISysMemberService memberService;

    @Override
    public SysMemberThirdPartyAuth findByOpenId(String openId) {
        if (StringUtils.isEmpty(openId)){
            return null;
        }
        return baseDao.findByOpenId(openId);
    }

    @Override
    public List<SysMemberThirdPartyAuth> findByUid(Long uid) {
        if (null == uid || uid < 0){
            return Collections.emptyList();
        }
        return baseDao.findByUserId(uid);
    }

    @Override
    public List<SysMemberThirdPartyAuth> findByUnionId(String unionId) {
        if (StringUtils.isEmpty(unionId)){
            return Collections.emptyList();
        }
        return baseDao.findByUnionId(unionId);
    }

    @Override
    public SysMemberThirdPartyAuth findByOpenIdAndUserId(String openId, Long uid) {
        if (StringUtils.isEmpty(openId) || null == uid){
            return null;
        }
        return baseDao.findByOpenIdAndUserId(openId,uid);
    }

    @Override
    public SysMemberThirdPartyAuth findByTypeAndOpenId(EnumThirdOauthType type, String openId) {
        if (StringUtils.isEmpty(openId) || null == type){
            return null;
        }
        return baseDao.findByTypeAndOpenId(type,openId);
    }

    @Override
    public SysMemberThirdPartyAuth findByTypeAndUserId(EnumThirdOauthType type, Long uid) {
        if (null == uid || null == type || uid < 1){
            return null;
        }
        return baseDao.findByTypeAndUserId(type,uid);
    }

    @Override
    public UserLoginResponse login(SysMemberThirdPartyAuth oauth){
        SysMemberThirdPartyAuth thirdOauth = findByOpenId(oauth.getOpenId());
        Long uid = thirdOauth.getUserId();
        SysMember member = null;
        if (null != uid && uid > 0){
            member = memberService.findById(uid);
        }
        if (null != member){
            member.setLastLoginTime(member.getLastLoginTime());
            member.setLastLoginHost(member.getLastLoginHost());
            member.setLoginCount(member.getLoginCount() + 1);
            member.setCurrentLoginTime(new Date());
            member.setLoginType(oauth.getType());
            member.setCurrentLoginHost(IpUtils.getIpAddr(WebUtil.getRequest()));
            memberService.save(member);
        }
        return memberService.setMemberToken(new UserLoginResponse(), member);
    }

    @Override
    public SysMember updateMobile(Long id, String mobile){
        memberService.updateMobile(id,mobile);
        return memberService.findById(id);
    }
}
